﻿using GalaSoft.MvvmLight;
using mal.WP7.GoogleAnalytics.Business.Repository;

namespace mal.WP7.GoogleAnalytics.ViewModels
{
    public class AccountViewModel : ViewModelBase
    {
        #region Fields
        private Account account;
        private EditAccountViewModel editAccountViewModel;
        private bool loginIsvalid;
        private bool passwordIsValid;
        private bool isValid;
        #endregion

        #region Construction / Destruction
        public AccountViewModel(EditAccountViewModel editAccountViewModel, Account account)
        {
            this.editAccountViewModel = editAccountViewModel;
            this.Account = account;
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public Account Account
        {
            get
            {
                return this.account;
            }

            private set
            {
                this.account = value;
                this.CheckLogin(this.Login);
                this.CheckPassword(this.Password);
            }
        }

        public string Login
        {
            get
            {
                return this.account.Login;
            }

            set
            {
                if (this.CheckLogin(value))
                {
                    this.account.Login = value;
                    RaisePropertyChanged("Login");

                    this.editAccountViewModel.Save();
                }
            }
        }

        public string Password
        {
            get
            {
                return this.account.Password;
            }

            set
            {
                if (this.CheckPassword(value))
                {
                    this.account.Password = value;
                    RaisePropertyChanged("Login");

                    this.editAccountViewModel.Save();
                }
            }
        }

        public bool PasswordIsValid
        {
            get
            {
                return this.passwordIsValid;
            }

            set
            {
                if (this.passwordIsValid != value)
                {
                    this.passwordIsValid = value;
                    RaisePropertyChanged("PasswordIsValid");
                }

                this.SetIsValid();
            }
        }

        public bool LoginIsvalid
        {
            get
            {
                return this.loginIsvalid;
            }

            set
            {
                if (this.loginIsvalid != value)
                {
                    this.loginIsvalid = value;
                    RaisePropertyChanged("LoginIsvalid");
                }

                this.SetIsValid();
            }
        }

        public bool IsValid
        {
            get
            {
                return this.isValid;
            }

            set
            {
                if (this.isValid != value)
                {
                    this.isValid = value;
                    RaisePropertyChanged("IsValid");
                }
            }
        }

        #endregion

        #region Methods

        private bool CheckLogin(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                this.LoginIsvalid = false;
                return this.LoginIsvalid;
            }

            this.LoginIsvalid = true;
            return this.LoginIsvalid;
        }

        private bool CheckPassword(string value)
        {
            this.PasswordIsValid = false;
            if (string.IsNullOrEmpty(value))
            {
                this.PasswordIsValid = false;
                return this.PasswordIsValid;
            }

            this.PasswordIsValid = true;
            return this.PasswordIsValid;
        }

        private void SetIsValid()
        {
            this.IsValid = this.LoginIsvalid && this.PasswordIsValid;
        }
        #endregion

        #region Events

        #endregion
    }
}
